package vn.student;

import java.util.ArrayList;
import vn.student.controller.ViewSemesterDetailController;
import vn.student.mylib.Constants;
import vn.student.mylib.Course;
import vn.student.mylib.ListCourseAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.RelativeLayout;

	/*
	
		Class name: ViewSemesterDetail.java
		Function: Day la lop ke thua lop Activity, la lop khoi tao cua so Activity dung de xem thong tin chi tiet mot hoc ki.
		
	*/

public class ViewSemesterDetail extends Activity implements View.OnClickListener{
	String id = null;
	ViewSemesterDetailController controller = null;
	boolean isEditSomething = false;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.semester_detail);
		RelativeLayout tmp = (RelativeLayout) findViewById(R.id.button01);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickBackButton();
			}
		});
		tmp = (RelativeLayout) findViewById(R.id.button02);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickEditButton();
			}
		});
		tmp = (RelativeLayout) findViewById(R.id.addNewCourseBtn);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickAddNewCourseButton();
			}
		});
		Intent intent = this.getIntent();
		id = intent.getExtras().getString("id");
		controller = new ViewSemesterDetailController(this,id);
		controller.showSemesterDetail(id);
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public void onClickBackButton(){
		if(isEditSomething)
			this.setResult(Activity.RESULT_OK);
		else
			this.setResult(Activity.RESULT_CANCELED);
		this.finish();
	}
	public void onClickEditButton(){
		Intent intent = new Intent(this,ViewEditSemesterDetailEditor.class);
		Bundle bundle = new Bundle();
		bundle.putString("id", id);
		bundle.putBoolean("isCreateNew", false);
		intent.putExtras(bundle);
		this.startActivityForResult(intent, Constants.ViewSemesterDetailEditorRequestCode);
	}
	private void onClickAddNewCourseButton(){
		Intent intent = new Intent(this,ViewCourseDetailEditor.class);
		Bundle bundle = new Bundle();
		bundle.putString("id", id);
		bundle.putBoolean("isCreateNew", true);
		bundle.putString("semesterID", id);
		intent.putExtras(bundle);
		this.startActivityForResult(intent, Constants.ViewAddNewCourseDetailEditorRequestCode);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==Constants.ViewAddNewCourseDetailEditorRequestCode){
			if(resultCode==RESULT_OK){
				controller.showSemesterDetail(id);
			}
		}
		if(requestCode==Constants.ViewCourseDetailEditorRequestCode){
			if(resultCode==RESULT_OK){
				controller.showSemesterDetail(id);
			}
		}
		if(requestCode==Constants.ViewSemesterDetailEditorRequestCode){
			if(resultCode==RESULT_OK){
				isEditSomething = true;
				controller.showSemesterDetail(id);
			}
		}
		if(requestCode==Constants.ViewCourseDetailRequestCode){
			if(resultCode==RESULT_OK){
				isEditSomething = true;
				controller.showCourseList();
			}
		}
	}
	@Override
    public boolean onContextItemSelected(MenuItem  aItem) {
    	AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) aItem.getMenuInfo();
    	ListView listCourseView = (ListView)this.findViewById(R.id.listCourseView);
    	Course course = ((ListCourseAdapter)listCourseView.getAdapter()).list.get(menuInfo.position);
    	ArrayList<Course> list = ((ListCourseAdapter)listCourseView.getAdapter()).list; 
         /* Switch on the ID of the item, to get what the user selected. */
         switch (aItem.getItemId()) {
              case Constants.SEMESTER_DETAIL_CONTEXTMENU_ADD_COURSE: 
            	  onClickAddNewCourseButton();
                  return true;
              case Constants.SEMESTER_DETAIL_CONTEXTMENU_EDIT_COURSE:
            	  Intent intent = new Intent(this,ViewCourseDetailEditor.class);
            	  Bundle bundle = new Bundle();
            	  bundle.putString("id", course.id);
            	  bundle.putBoolean("isCreateNew", false);
            	  intent.putExtras(bundle);
            	  this.startActivityForResult(intent, Constants.ViewCourseDetailEditorRequestCode);
            	  return true;
              case Constants.SEMESTER_DETAIL_CONTEXTMENU_DELETE_COURSE:
            	  if(controller.removeCourse(course.id)){
            		  list.remove(course);
            		  if(list!=null&&list.size()==0){
            				 ((RelativeLayout) this.findViewById(R.id.addNewCourseBtn)).setBackgroundResource(R.drawable.class_row_all);
            		  }
            		  listCourseView.setAdapter( new ListCourseAdapter(this,list));
            	  }
            	  return true;
         }
         return false;
    }

}
